package ldh.im.admin.rest;

import ldh.common.PageResult;
import ldh.im.base.constant.ArticleStatus;
import ldh.im.base.constant.CategoryStatus;
import ldh.im.base.pojo.CmsArticle;
import ldh.im.base.pojo.CmsCategory;
import ldh.im.base.pojo.ImGroup;
import ldh.im.base.pojo.ImUser;
import ldh.im.base.pojo.where.CmsArticleWhere;
import ldh.im.base.pojo.where.CmsCategoryWhere;
import ldh.im.base.service.CmsArticleService;
import ldh.im.base.service.CmsCategoryService;
import ldh.im.admin.ResponseResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/client")
public class CmsController {

    @Resource(name = "cmsArticleService")
    private CmsArticleService cmsArticleService;
    @Resource(name = "cmsCategoryService")
    private CmsCategoryService cmsCategoryService;

    @GetMapping(path="/cms/init")
    public ResponseResult init(@RequestParam("tenantId")Long tenantId,
             @RequestParam(name="changeDate", defaultValue = "2021-01-01") Date changeDate, @RequestParam(name="page", defaultValue = "1")Integer page) {
        PageResult<CmsArticle> changeArticles = getCmsArticlePageResult(tenantId, changeDate, page);

        PageResult<CmsCategory> changeCategorys = getCmsCategoryPageResult(tenantId, changeDate, page);

        return ResponseResult.build().put("changeArticles", changeArticles).put("changeCategories", changeCategorys);
    }

    @GetMapping(path="/cms/getChangeCategories")
    public ResponseResult getChangeCategories(@RequestParam("tenantId")Long tenantId,
                                            @RequestParam(name="changeDate", defaultValue = "2021-01-01") Date changeDate, @RequestParam(name="page", defaultValue = "1")Integer page) {

        PageResult<CmsCategory> changeCategorys = getCmsCategoryPageResult(tenantId, changeDate, page);
        return ResponseResult.build().put("changeCategories", changeCategorys);
    }

    @GetMapping(path="/cms/getChangeArticles")
    public ResponseResult getChangeArticles(@RequestParam("tenantId")Long tenantId,
                                            @RequestParam(name="changeDate", defaultValue = "2021-01-01") Date changeDate, @RequestParam(name="page", defaultValue = "1")Integer page) {
        PageResult<CmsArticle> changeArticles = getCmsArticlePageResult(tenantId, changeDate, page);
        return ResponseResult.build().put("changeArticles", changeArticles);
    }


    private PageResult<CmsCategory> getCmsCategoryPageResult(Long tenantId, Date changeDate, Integer page) {
        CmsCategoryWhere cmsCategoryWhere = new CmsCategoryWhere();
        cmsCategoryWhere.setTenantId(tenantId);
        cmsCategoryWhere.setStartUpdateTime(changeDate);
        cmsCategoryWhere.setPageNo(page);
        cmsCategoryWhere.setOrder("cc.update_time desc");
        return cmsCategoryService.findByCmsCategoryWhere(cmsCategoryWhere);
    }

    private PageResult<CmsArticle> getCmsArticlePageResult(Long tenantId, Date changeDate, Integer page) {
        CmsArticleWhere cmsArticleWhere = new CmsArticleWhere();
        cmsArticleWhere.setTenantId(tenantId);
        cmsArticleWhere.setStartUpdateTime(changeDate);
        cmsArticleWhere.setPageNo(page);
        cmsArticleWhere.setPageSize(10);
        cmsArticleWhere.setOrder("ca.update_time desc");
        return cmsArticleService.findByCmsArticleWhere(cmsArticleWhere);
    }
}
